/**
 * 客户端使用Ajax技术获取服务端数据
 * 多个para参数必须使用`间隔
 * @author dyh 2011-02-05
 */
package com.zjht.gmcc.servlet.permit;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zjht.gmcc.bean.provide.Parameter;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParam;

public class AjaxProType extends HttpServlet {

	private static final long serialVersionUID = 1135302774945453643L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doAll(request, response);
	}
	public void toJson(Object obj,HttpServletResponse response) throws Exception{
		Gson gson = new Gson();

		String result = gson.toJson(obj);

	
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");

		PrintWriter out = response.getWriter();
		out.write(result);

		out.flush();
		out.close();
	}
	private void doAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=GBK");
		response.setHeader("Cache-Control", "no-cache");
		request.setCharacterEncoding("UTF-8");
		int i_flag = 0;
		// ---参数
		
		String pareID= request.getParameter("pareID");
		String levelid= request.getParameter("levelid");
	

		String s_sql = null;
		PreparedStatement ps,ps2 = null;
		ResultSet rs,rs2 = null;
		
		String f_id="";
		
		//System.out.println(pareID);
		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			String s="";
			if(levelid.equals("1")){
			s_sql="select * from PRODUCTTYPE where LEVELID=2 and  PARENTTYPEID=?";
			ps=myJDBC.prepareStatement(s_sql);
			
				ps.setString(1, pareID);
				
				rs=ps.executeQuery();
				while(rs.next()){
					s+=" <div style= \"white-space:nowrap; width:'158'\" name=\"spmi"+pareID+"_\" ><div style=\"float: left; display: inline;\"><input type=\"checkbox\" name=\"mi"+rs.getString("TYPEID")+"\" id=\"mi"+rs.getString("TYPEID")+"\"  value='"+rs.getString("TYPEID")+"'  />"+rs.getString("TYPENAME")+" </div><div style=\"float: right; display: inline;\"><input type=\"button\" name=\"mid\" id=\"mid"+rs.getString("TYPEID")+"\"  value='  >>  '  onclick=\"permit.check_son('"+rs.getString("TYPEID")+"')\"  /></div></div>";
					//s+=" <div id=\"menu_sub_"+rs.getString("TYPEID")+"\" class=\"sub_menu\" style=\"display:none\" >";
					//ps2.setString(1, rs.getString("TYPEID"));
					//rs2=ps2.executeQuery();
					//while(rs2.next()){
					//s+="<ul><li> <input type=\"checkbox\" name=\"sm"+rs.getString("TYPEID")+"\" id=\"operID\"  value="+rs2.getString("TYPEID")+" checked onclick=\"permit.check_father("+rs.getString("TYPEID")+",this)\" />"+rs2.getString("TYPENAME")+"</li></ul>";
					//}
					//s+="</div>";
					//s+=" <div name=\"spmi"+f_id+"_spsm"+pareID+"\" ><input type=\"checkbox\" name=\"sm"+pareID+"\" id=\"operID\"  value="+rs.getString("TYPEID")+"/>"+rs.getString("TYPENAME")+"</div>";
					
				}
				
				toJson(s,response);
			}else if(levelid.equals("2")){
				s_sql="select * from PRODUCTTYPE where LEVELID=3 and  PARENTTYPEID=?";
				ps=myJDBC.prepareStatement(s_sql);
				ps.setString(1, pareID);
				rs=ps.executeQuery();
				
				s_sql="select PARENTTYPEID from PRODUCTTYPE where LEVELID=2 and  TYPEID=?";
				ps2=myJDBC.prepareStatement(s_sql);
				ps2.setString(1, pareID);
				rs2=ps2.executeQuery();
				if(rs2.next()){
					f_id=rs2.getString(1);
				}
				while(rs.next()){
					s+=" <div name=\"spmi"+f_id+"_spsm"+pareID+"\" ><input type=\"checkbox\" name=\"sm"+pareID+"\" id=\"operID\"  value='"+rs.getString("TYPEID")+"'/>"+rs.getString("TYPENAME")+"</div>";
					
				}
				toJson(s,response);
			}else if(levelid.equals("5")){
				List<Parameter> pr=new ArrayList<Parameter>();
				s_sql="select p.providername from provider p where (UPPER(p.providercode) like UPPER(?) or UPPER(p.providername) like UPPER(?)) and status!=0 ORDER BY p.providerid";
				ps=myJDBC.prepareStatement(s_sql);
				ps.setString(1, "%"+pareID+"%");
				ps.setString(2, "%"+pareID+"%");
				rs=ps.executeQuery();
				
				while(rs.next()){
					Parameter  pa= new Parameter();
					pa.setParaid(rs.getString(1));
					pa.setParname(rs.getString(1));
					System.out.println(rs.getString(1));
					pr.add(pa);
				}
				
				toJson(pr,response);
				pr=null;
			}else if(levelid.equals("6")){
				List<Parameter> pr=new ArrayList<Parameter>();
				s_sql = "select paraid,paraname from PARAMETER where PARENTID=? and status=0 order by LISTNUM";ps=myJDBC.prepareStatement(s_sql);
				ps.setString(1, pareID);
			
				rs=ps.executeQuery();
				
				while(rs.next()){
					Parameter  pa= new Parameter();
					pa.setParaid(rs.getString(1));
					pa.setParname(rs.getString(2));
					//System.out.println(rs.getString(1));
					pr.add(pa);
				}
				
				toJson(pr,response);
				pr=null;
			}
			
			
			
			
			//System.out.println(s);
		
			
			
			} catch (Exception e) {
			e.printStackTrace();
			System.err.println("sql=" + s_sql);
		} finally {
			MyJDBC.close(myJDBC);
		}
	}
}
